package com.atzzy.ysmall.ysmall_api.webScoket;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;

import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author JAVAZeng
 * @Date 2022/4/8 19:47
 * @Version 1.0
 */
@ConditionalOnClass(value = WebSocketConfig.class)
@Component
@ServerEndpoint("/webScoket/{orderId}")
public class WebSocketServer {

    private static ConcurrentHashMap<String,Session> sessionMap = new ConcurrentHashMap<>();
    //前端发送请求 建立webScoket连接
    @OnOpen
    public void open(@PathParam("orderId") String orderId, Session session){
        System.out.println("------------websocket"+orderId);
        sessionMap.put(orderId,session);
    }
    //前端关闭页面或者主动关闭websocket 都会执行close
    @OnClose
    public void close(@PathVariable("orderId") String orderId){
        sessionMap.remove(orderId);
    }
}
